package com.courierplease.converter.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.courierplease.converter.constant.Constants;
import com.courierplease.converter.cpreq.CpQuoteReq;
import com.courierplease.converter.cpreq.Items;
import com.courierplease.converter.cpresp.CpQuoteResp;
import com.courierplease.converter.cpresp.Data;
import com.courierplease.converter.req.CourierAccount;
import com.courierplease.converter.req.Parcels;
import com.courierplease.converter.req.QuoteReq;
import com.courierplease.converter.resp.QuoteResp;
import com.courierplease.converter.service.IQuoteService;
import com.courierplease.converter.utils.HttpClientUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created By shenhaitao on 2021/3/29
 */
@Service
public class QuoteServiceImpl implements IQuoteService {

    @Value("${cpapi.url}")
    private volatile String CPAPI;

    public List<QuoteResp> postForQuote(QuoteReq quoteReq){
        CpQuoteReq cpQuoteReq = constructCpQuoteReq(quoteReq);
        CourierAccount courierAccount = quoteReq.getCourierAccount();
        String username = courierAccount.getUsername();
        String password = courierAccount.getPassword();
        String base64 = Base64.encodeBase64String((username+":"+password).getBytes());
        String auth = Constants.BASIC+base64;
        String cpresp = HttpClientUtils.doPostJsonParam(CPAPI, JSONObject.toJSONString(cpQuoteReq), auth);
        CpQuoteResp cpQuoteResp = JSONObject.parseObject(cpresp, CpQuoteResp.class);
        List<QuoteResp> list = constructQuoteResp(cpQuoteResp);
        return list;
    }

    /**
     * 构建响应参数
     * Build response parameters
     * @param cpQuoteResp
     * @return
     */
    public List<QuoteResp> constructQuoteResp(CpQuoteResp cpQuoteResp){
        List<QuoteResp> list = new ArrayList<>();
        List<Data> data = cpQuoteResp.getData();
        for (Data dt : data) {
            QuoteResp quoteResp = new QuoteResp();
            quoteResp.setRateCardCode(dt.getRateCardCode());
            BigDecimal amount = new BigDecimal(dt.getCalculatedFuelCharge()).add(new BigDecimal(dt.getCalculatedFreightCharge()));
            quoteResp.setAmount(amount.toString());
            list.add(quoteResp);
        }
        return list;
    }
    /**
     * 构建cp请求参数
     * Build cp request parameters
     * @param quoteReq
     * @return
     */
    public CpQuoteReq constructCpQuoteReq(QuoteReq quoteReq){
        CpQuoteReq cpQuoteReq = new CpQuoteReq();
        cpQuoteReq.setFromSuburb(quoteReq.getShipFrom().getShipFrom_suburb());
        cpQuoteReq.setFromPostcode(quoteReq.getShipFrom().getShipFrom_postcode());
        cpQuoteReq.setToSuburb(quoteReq.getShipTo().getShipTo_suburb());
        cpQuoteReq.setToPostcode(quoteReq.getShipTo().getShipTo_postcode());
        List<Items> items = new ArrayList<>();
        List<Parcels> parcels = quoteReq.getParcels();
        for (Parcels parcel : parcels) {
            Items item = new Items();
            item.setHeight(parcel.getHeight());
            item.setLength(parcel.getLength());
            item.setWidth(parcel.getWidth());
            item.setQuantity(parcel.getQty());
            item.setPhysicalWeight("0.01");
            items.add(item);
        }
        cpQuoteReq.setItems(items);
        return cpQuoteReq;
    }
}
